package com.zzc.emotisenseclassroom.controller.user;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zzc.common.response.ZzcResponse;
import com.zzc.emotisenseclassroom.common.ResponseCodeEnum;
import com.zzc.emotisenseclassroom.domain.user.service.ClassService;
import com.zzc.emotisenseclassroom.domain.user.service.SchoolInfoService;
import com.zzc.emotisenseclassroom.domain.user.service.UserService;
import com.zzc.emotisenseclassroom.domain.user.service.VerificationCodeService;
import com.zzc.emotisenseclassroom.dto.user.UserDTO;
import com.zzc.emotisenseclassroom.pojo.UserLogin;
import com.zzc.emotisenseclassroom.pojo.UserRegister;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright Zzc
 * FileName: LoginInfoController
 * Author:   Zzc
 * Date:     2024/4/1417:46
 * Description:用户登录注册权限控制器
 * hISTORY:
 * <author>  <time>     <version>    <desc>
 * 作者姓名   修改时间       版本号       描述
 * Zzc                   1.0.0
 */
@RestController
@RequestMapping("/user")
public class UserInfoController {
    private static final Logger LOG = LogManager.getLogger(UserInfoController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private VerificationCodeService verificationCodeService;
    @Autowired
    private SchoolInfoService schoolInfoService;
    @Autowired
    private ClassService classService;

    @PostMapping("/login")
    public ZzcResponse userLogin(HttpServletRequest httpServletRequest, @RequestBody UserLogin userLogin){
        if (!verificationCodeService.rightValidateCode(httpServletRequest, userLogin.getValidateCode())){
            return ZzcResponse.success(ResponseCodeEnum.WRONG_VALIDATE_COD);
        }

        UserDTO userInfo = userService.login(userLogin);
        LOG.info("用户信息{}", JSON.toJSON(userInfo));
        if (userInfo == null){
            return ZzcResponse.success(ResponseCodeEnum.PASSWORD_FALSE);
        }else {
            return ZzcResponse.success(ResponseCodeEnum.LOGIN_SUCCESS, userInfo);
        }
    }

    @PostMapping("/register")
    public ZzcResponse userRegister(@RequestBody UserRegister userRegister, HttpServletRequest httpServletRequest){
        if (!verificationCodeService.rightValidateCode(httpServletRequest, userRegister.getValidateCode())){
            return ZzcResponse.success(ResponseCodeEnum.WRONG_VALIDATE_COD);
        }

        UserDTO userInfo = userService.register(userRegister);
        if (userInfo == null){
            return ZzcResponse.success(ResponseCodeEnum.DUPLICATE_ACCOUNT_ID);
        }
        return ZzcResponse.success(ResponseCodeEnum.REGISTER_SUCCESS, userInfo);
    }

    @PostMapping("/getSchoolList")
    public ZzcResponse getSchoolInfoList(String schoolName){
        return ZzcResponse.success(ResponseCodeEnum.SEARCH_SUCCESS, schoolInfoService.getSchoolList(schoolName));
    }

    @PostMapping("/getClassList")
    public ZzcResponse getClassInfoList(@RequestBody String schoolId){
        JSONObject jsonObject = JSON.parseObject(schoolId);
        return ZzcResponse.success(ResponseCodeEnum.SEARCH_SUCCESS, classService.getClassInfoList(jsonObject.getLong("schoolId")));
    }

    /*@PostMapping("/getClassList")
    public ZzcResponse getClassInfoList(@RequestBody Long schoolId){
        return ZzcResponse.success(ResponseCodeEnum.SEARCH_SUCCESS, classService.getClassInfoList(schoolId));
    }*/

    @PostMapping("/updateUserInfo")
    public ZzcResponse updateUserInfo(@RequestBody UserDTO userInfo){
        return ZzcResponse.success(ResponseCodeEnum.UPDATE_USER_INFO_SUCCESS, userService.updateUserInfo(userInfo));
    }


}
